#!/usr/bin/env python

from transaction_class import Transaction

#{{{def _create_dataset ( start_point, lines, rows, tot_rows ):
def _create_dataset ( start_point, lines, rows, tot_rows ):
    res = {}

    count = 0
    for v in rows:
        v [ "_pos" ] = start_point + count
        res [ "row%d" % count ] = v
        count += 1

    res [ 'rows' ] = tot_rows
    res [ 'from_row' ] = start_point
    res [ 'to_row' ] = start_point + count
    res [ 'lines' ] = lines

    return res
#}}}


def get_cfg ( liwe ):
	p = Transaction ( liwe )
	transaction_cfg = {
			'debug' : p.cfg.get ( 'debug', 1 ),
			'pp_url' : p.cfg.get ( 'paypal.url', 'https://www.paypal.com/cgi-bin/webscr' ),
			'pp_mail' : p.cfg.get ( 'paypal.business_mail', ''),
			'pp_cmd' : p.cfg.get ( 'paypal.cmd', '_cart' ),
			'pp_currency' : p.cfg.get ( 'paypal.currency_code', 'EUR' ),
			'pp_location' : p.cfg.get ( 'paypal.location', 'IT' ), 
			'pp_cancel' : p.cfg.get ( 'paypal.cancel_url', '' ), 
			'pp_confirm' : p.cfg.get ( 'paypal.confirm_url', '' ), 
			'pp_notify' : p.cfg.get ( 'paypal.notify_url', '' ) 
		}
	if transaction_cfg [ 'debug' ]:
		transaction_cfg [ 'pp_url' ] = p.cfg.get ( 'paypal.debug_url', 'https://www.sandbox.paypal.com/cgi-bin/webscr' )
		transaction_cfg [ 'pp_mail' ] = p.cfg.get ( 'paypal.debug_mail', '' )
	
	return { 'cfg' : transaction_cfg }
		

def get_templates ( liwe ):
	p = Transaction ( liwe )
	return { "templates" : p.get_templates () }

def get_hash ( liwe, custom ):
	p = Transaction ( liwe )
	return { 'hash' : p.get_hash ( custom ) }

def paypal_cback ( liwe, kwargs = {} ):
        """
        :INPUT:
        """
        p = Transaction ( liwe )
        p.paypal_cback ( kwargs )


def get_kind ( liwe, id_kind = 0 ):
	"""
	:INPUT:
		- id_kind:            SAFE_NUMBER 
	"""
	p = Transaction ( liwe )
	return { "kind" : p.get_kind ( id_kind = id_kind ) }


def get_charge ( liwe, id_kind = 0, totale = 0 ):
	"""
	:INPUT:
		- id_kind:	SAFE_NUMBER 
		- totale:	SAFE_NUMBER 
	"""
	p = Transaction ( liwe )
	return p.get_charge ( id_kind = id_kind, totale = totale )
	

def list_kind ( liwe, descr = '', visible = -1, module = '', quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0 ):
	"""
	:INPUT:
		- descr:		SAFE_DB_STRING
		- visible:		SAFE_NUMBER
		- module:		SAFE_DB_STRING
		- quant:		SAFE_NUMBER
		- _X_LINES:     SAFE_NUMBER
		- _X_START_POINT:   SAFE_NUMBER
		- _X_PAGE:      SAFE_NUMBER
	""" 
    
	use_dataset = 0

	if _X_LINES != -1:
		quant = _X_LINES
		use_dataset = 1

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * quant

	p = Transaction ( liwe )
	resp = p.list_kind ( descr = descr, visible = visible, module = module, quant = quant, start_point = start_point, do_count = use_dataset )


	rows = resp [ 'rows' ]
	tot_rows =  resp [ 'tot_rows' ]

	if not use_dataset:
		return { 'kind': rows }

	return _create_dataset ( start_point, quant, rows, tot_rows )

def custom_add ( liwe, module, key, values, is_list = False ):
	"""
	"""

	p = Transaction ( liwe )
	p.custom_add ( module, key, values, is_list )


def custom_del ( liwe, module, key ):
	"""
	"""
	p = Transaction ( liwe )

	p.custom_del ( module, key )

def custom_del_module ( liwe, module ):
	"""
	"""
	p = Transaction ( liwe )

	p.custom_del_module ( module )

def custom_serialize ( liwe ):
	"""
	"""
	p = Transaction ( liwe )

	return { "custom" : p.custom_serialize () }

def custom_get_url ( liwe ):
	"""
	"""
	p = Transaction ( liwe )

	return { "custom" : p.custom_get_url () }

def custom_parse_url ( liwe, txt ):
	"""
	"""
	p = Transaction ( liwe )

	return { "custom" : p.custom_parse_url ( txt ) }
